package com.yang.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
 * 集合工具包
 * 
 * @author 张恒阳
 *
 */
public class CollectionUtil {

	public static Boolean isBlank(Collection<? extends Object> list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

	public static <T> Boolean isBlank(T[] ts) {
		if (ts == null || ts.length == 0) {
			return true;
		}
		return false;
	}

	public static <T> Boolean isNotBlank(T[] ts) {
		if (ts != null && ts.length > 0) {
			return true;
		}
		return false;
	}

	public static Boolean isNotBlank(Collection<? extends Object> collageIds) {
		if (collageIds != null && !collageIds.isEmpty()) {
			return true;
		}
		return false;
	}

	public static <K, V> Boolean isNotBlank(Map<K, V> map) {
		if (map != null && !map.isEmpty()) {
			return true;
		}
		return false;
	}

	public static <K, V> Boolean isBlank(Map<K, V> map) {
		if (map == null || map.isEmpty()) {
			return true;
		}
		return false;
	}

	/**
	 * 转化为list
	 * 
	 * @param list
	 * @return
	 */
	public static <T> List<T> parseList(Collection<T> list) {
		if (list == null) {
			return null;
		}
		if (list instanceof List) {
			return (List<T>) list;
		}
		List<T> result = new ArrayList<T>();
		result.addAll(list);
		return result;
	}

	/**
	 * 转化为set
	 * 
	 * @param collageIds
	 * @return
	 */
	public static <T> Set<T> parseSet(Collection<T> collageIds) {
		if (collageIds == null) {
			return null;
		}
		if (collageIds instanceof Set) {
			return (Set<T>) collageIds;
		}
		Set<T> set = new HashSet<T>(collageIds.size());
		for (T t : collageIds) {
			set.add(t);
		}
		return set;
	}

	/**
	 * 创建一个集合 ( 不添加 null值)
	 * 
	 * @param collageIds
	 * @return
	 */
	@SafeVarargs
	public static <T> List<T> createList(T... ts) {
		if (ts == null || ts.length < 1) {
			return null;
		}
		List<T> list = new ArrayList<>();
		for (T t : ts) {
			if (t == null) {
				continue;
			}
			list.add(t);
		}
		if (isBlank(list)) {
			return null;
		}
		return list;
	}

	/**
	 * 创建一个set ( 不添加 null值)
	 * 
	 * @param collageIds
	 * @return
	 */
	@SafeVarargs
	public static <T> Set<T> createSet(T... ts) {
		if (ts == null || ts.length < 1) {
			return null;
		}
		Set<T> set = new HashSet<>();
		for (T t : ts) {
			if (t == null) {
				continue;
			}
			set.add(t);
		}
		if (isBlank(set)) {
			return null;
		}
		return set;
	}

	/**
	 * 去除重复
	 * 
	 * @param collageIds
	 * @return
	 */
	public static <T> void distinct(Collection<T> list) {
		if (isNotBlank(list)) {

			Set<T> set = new HashSet<T>();
			for (Iterator<T> it = list.iterator(); it.hasNext();) {
				if (!set.add(it.next())) {
					it.remove();
				}
			}
		}

	}

	/**
	 * 去除null
	 * 
	 * @param collageIds
	 * @return
	 */
	public static <T> void removeNull(Collection<T> list) {
		if (isNotBlank(list)) {

			for (Iterator<T> it = list.iterator(); it.hasNext();) {
				if (it.next() == null) {
					it.remove();
				}
			}
		}
	}

	/**
	 * 去重并且去重null
	 * 
	 * @param collageIds
	 * @return
	 */
	public static <T> void distinctAndremoveNull(Collection<T> list) {
		if (isNotBlank(list)) {
			Set<T> set = new HashSet<>();
			for (Iterator<T> it = list.iterator(); it.hasNext();) {
				T next = it.next();
				if (next == null || !set.add(next)) {
					it.remove();
				}
			}

		}
	}

	@SuppressWarnings("unchecked")
	public static <T, E> List<E> getPropertyList(Collection<T> coll, String property, Class<E> clazz) {
		List<E> list = new ArrayList<>(coll.size());
		if ( StringUtils.isBlank(property) || clazz == null) {
			return list;
		}
		String methodName = "get" + property.substring(0, 1).toUpperCase() + property.substring(1, property.length());

		Method method = null;
		for (T t : coll) {
			try {
				if (method == null) {
					if ((method = t.getClass().getMethod(methodName)) == null) {
						return list;
					}
				}
				Object object = method.invoke(t);
				if (object != null && object.getClass() == clazz) {
					list.add((E) object);
				}
			} catch (Exception e) {
			}
		}
		return list;
	}

}
